package com.msb.dongbao.portal.web.controller.studyCaptcha;

import com.baomidou.kaptcha.Kaptcha;
import com.msb.dongbao.portal.web.custom.MyGoogleKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author: mayu
 * @date: 2021年05月28日 14:55
 */
@RestController
@RequestMapping("/kaptcha")
public class KaptchaController {

    @Autowired
    Kaptcha kaptcha;

    @Autowired
    MyGoogleKaptcha myGoogleKaptcha;

    @GetMapping("/generator")
    public void generatorCode(HttpServletRequest request, HttpServletResponse response) {
        kaptcha.render();
    }

    @GetMapping("/verify")
    public String verify(String verifyCode, HttpServletRequest request) {
        boolean flag = kaptcha.validate(verifyCode, 20);
        if (flag) {
            return "通过";
        }
        return "不通过";
    }

    @GetMapping("/generator-my")
    public void generatorCodeMy(HttpServletRequest request, HttpServletResponse response) {
        myGoogleKaptcha.render();
    }

    @GetMapping("/verify-my")
    public String verifyMy(String verifyCode, HttpServletRequest request) {
        boolean flag = myGoogleKaptcha.validate(verifyCode, 20);
        if (flag) {
            return "通过";
        }
        return "不通过";
    }

    @GetMapping("/generator-base64")
    public Map<String, String> generatorCodeBase64(HttpServletRequest request, HttpServletResponse response) {
        return myGoogleKaptcha.renderBase64();
    }

    @GetMapping("/verify-base64")
    public String verifyBase64(String verifyCode, String uuid, HttpServletRequest request) {
        boolean flag = myGoogleKaptcha.validateBase64(verifyCode, uuid, 20);
        if (flag) {
            return "通过";
        }
        return "不通过";
    }

}
